//
//  DLPhoneAndMsgUtils.m
//  DGGUIKit
//
//  Created by 伍明鹏 on 2019/12/26.
//

#import "DLPhoneAndMsgUtils.h"
#import "TipTool.h"
#import <WebKit/WebKit.h>

typedef NS_ENUM(NSInteger, URLType) {
    URLTypePhone,
    URLTypeSMS
};
@implementation DLPhoneAndMsgUtils

+ (void)callWithPhone:(NSString *)phone
{
    [self openURLWithType:URLTypePhone phone:phone];
}

+ (void)sendMessageWithPhone:(NSString *)phone
{
    [self openURLWithType:URLTypeSMS phone:phone];
}

+ (void)openURLWithType:(URLType)type phone:(NSString *)phone
{
    //    if (![Helper validateMobile:phone]) {
    //        [TipTool showMasErrorTip:@"非法电话号码" superView:KMyWindow];
    //        return;
    //    }
    
    if( [[UIDevice currentDevice].model isEqualToString:@"iPhone"])
    {
        
        NSURL *telURL;
        
        switch (type) {
            case URLTypePhone:
                telURL =[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phone]];
                break;
                
            case URLTypeSMS:
                telURL =[NSURL URLWithString:[NSString stringWithFormat:@"sms:%@",phone]];
                break;
        }
        
        CGFloat version = [[[UIDevice currentDevice]systemVersion]floatValue];
        
        if (version >= 10.0) {
            /// 大于等于10.0系统使用此openURL方法
            [[UIApplication sharedApplication] openURL:telURL options:@{} completionHandler:nil];
        } else {
            WKWebView *phoneCallWebView = [WKWebView new];
            phoneCallWebView.UIDelegate = self;
            phoneCallWebView.navigationDelegate = self;
            [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:telURL]];
            [[DGGCommonSDKUtils currentViewController].navigationController.view addSubview:phoneCallWebView];
        }
    }
    else
    {
        kWindow
        [TipTool showMasErrorTip:@"非电话设备，不能打电话！" superView:window];
    }
}

@end
